﻿namespace EFS.Common.Config.Components
{
	/// <summary>
	/// Configuration section
	/// </summary>
	public class Section : System.Configuration.ConfigurationSection
	{
		/// <summary>
		/// Returns section by name
		/// </summary>
		public static EFS.Common.Config.Components.Section Open(System.String sectionPath)
		{
			// app config
			return (EFS.Common.Config.Components.Section)System.Configuration.ConfigurationManager.GetSection(sectionPath);
		}

		/// <summary>
		/// To String
		/// </summary>
		/// <returns></returns>
		public override System.String ToString()
		{
			return this.SectionInformation.SectionName;
		}

		/// <summary>
		/// Returns section by name
		/// </summary>
		public static EFS.Common.Config.Components.Section Open(System.String configFilePath, System.String sectionPath)
		{
			// open config
			System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(new System.Configuration.ExeConfigurationFileMap
			{
				ExeConfigFilename = configFilePath
			}, System.Configuration.ConfigurationUserLevel.None);

			// check
			if (null == configuration)
			{
				throw new System.Exception("Could not open configuration file.");
			}

			// get section
			return (EFS.Common.Config.Components.Section)configuration.GetSection(sectionPath);
		}

		/// <summary>
		/// Plain
		/// </summary>
		[System.Configuration.ConfigurationProperty("Plain")]
		public EFS.Common.Config.Components.Elements.ElementCollection Plain
		{
			get
			{
				return (EFS.Common.Config.Components.Elements.ElementCollection)this["Plain"] ?? new EFS.Common.Config.Components.Elements.ElementCollection();
			}
		}
	}
}
